package com.achievo.know.application.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.achievo.know.application.CategoryApplication;
import com.achievo.know.domain.CateContentRepository;
import com.achievo.know.domain.Category;
import com.achievo.know.domain.CategoryContent;
import com.achievo.know.domain.CategoryRepository;
import com.achievo.know.domain.User;
import com.achievo.know.pojo.JsonCategory;

@Service
public class CategoryApplicationImpl implements CategoryApplication {
	
	@Autowired
	private CategoryRepository categoryRepository;
	
	@Autowired
	private CateContentRepository cateContentResp;
	
	

	public CateContentRepository getCateContentResp() {
		return cateContentResp;
	}

	public void setCateContentResp(CateContentRepository cateContentResp) {
		this.cateContentResp = cateContentResp;
	}

	public CategoryRepository getCategoryRepository() {
		return categoryRepository;
	}

	public void setCategoryRepository(CategoryRepository categoryRepository) {
		this.categoryRepository = categoryRepository;
	}
	
	@Override
	@Transactional
	public void add(Category c) {
		if(c.getParent()==null){
			c.setPosition(getRoots().size()+1);
		}else{
			c.setPosition(c.getParent().getChildren().size()+1);
		}
		c.add();
	}
	
	@Override
	public Category get(Long id) {
		return categoryRepository.get(id);
	}
	
	@Override
	public List<Category> getRoots() {
		return categoryRepository.getRoots();
	}
	
	@Override
	public List<JsonCategory> getCates(List<Long> cateIds) {
		return createCate(cateIds);
	}
	
	@Override
	public Category getParent(Category subCate) {
		if(subCate.getParent()==null)
			return null;
		return subCate.getParent();
	}
	

	@Override
	public CategoryContent getDesc(Long id) {
		return (CategoryContent) cateContentResp.getSingleResult("from CategoryContent c where c.cateId=?", new Object[]{id});
	}

	
	@Override
	@Transactional
	public void saveDesc(CategoryContent temp) {
		cateContentResp.save(temp);
	}

	private List<JsonCategory> createCate(List<Long> list){
		List<JsonCategory> AllRoot=new ArrayList<JsonCategory>();
		List<Category> roots=getRoots();
		for(Category root:roots){//一级
			if(list!=null && list.size()>0){
				if(!list.contains(root.getId())){
					continue;
				}
			}
			JsonCategory croot=create(root,list,true);
			AllRoot.add(croot);
		}
		return AllRoot;
	}
	
	private JsonCategory create(Category child,List<Long> list,boolean root){
		JsonCategory cchild=new JsonCategory();
		cchild.setText(child.getName());
		cchild.setId(child.getId());
		cchild.setPosition(child.getPosition());
		cchild.setShowNav(child.isShowInNav());
		cchild.setRoot(root);
		List<Category> children1=child.getChildren();
		if(children1.size()>0){
			List<JsonCategory> l121=new ArrayList<JsonCategory>();
			for(Category child1:children1){
				if(list!=null && list.size()>0){
					if(!list.contains(child1.getId())){
						continue;
					}
				}
				JsonCategory cchild1=create(child1,list,false);
				l121.add(cchild1);
			}
			cchild.setChildren(l121);
		}
		return cchild;
	}

	@Override
	public String getJson(List<Long> list) {
		List<JsonCategory> AllRoot=createCate(list);
		try {
			return JSONUtil.serialize(AllRoot, null, null, false, true);
		} catch (JSONException e) {
			return "";
		}
	}
	
	
	
	@Override
	public String getJsonWithRoot(List<Long> list) {
		List<JsonCategory> roots=new ArrayList<JsonCategory>();
		JsonCategory croot=new JsonCategory();
		croot.setText("根");
		croot.setId(999L);
		croot.setPosition(0);
		croot.setShowNav(false);
		croot.setRoot(true);
		croot.setIsexpand(true);
		List<JsonCategory> AllRoot=createCate(list);
		croot.setChildren(AllRoot);
		roots.add(croot);
		try {
			return JSONUtil.serialize(roots, null, null, false, true);
		} catch (JSONException e) {
			return "";
		}
	}

	@Override
	public List<Long> listAllIds(Long cateId) {
		List<Long> addIds=new ArrayList<Long>();
		addIds.add(cateId);
		Category root=get(cateId);
			List<Category> children=root.getChildren();
			if(children.size()>0){
				for(Category child:children){//二级
					addIds.add(child.getId());
					List<Category> children1=child.getChildren();
					if(children1.size()>0){
						for(Category child1:children1){//三级
							addIds.add(child1.getId());
						}
					}
				}
			}
		return addIds;
	}

	@Override
	@Transactional
	public void deleteBatch(String[] idA) {
		for(String id:idA){
			Category c=categoryRepository.get(Long.parseLong(id));
			removecate(c);
		}
		
	}
	
	private void removecate(Category c){
		if(c!=null){
			List<Category> ch=c.getChildren();
			if(ch!=null){
				for(Category cc:ch){
					removecate(cc);
				}
			}
			categoryRepository.removeCategory(c);
		}
	}

	@Override
	@Transactional
	public void save(Category cate) {
		Category c=get(cate.getId());
		c.setName(cate.getName());
		c.setPosition(cate.getPosition());
		c.setShowInNav(cate.isShowInNav());
		categoryRepository.save(c);
	}

	@Override
	@Transactional
	public void changePos(String id, String toId, String type) {
		Category cur=get(Long.parseLong(id));
		Category toCate=get(Long.parseLong(toId));
		if("0".equals(type)){//之前
			cur.setParent(toCate.getParent());
			int top=toCate.getPosition();
			Category parent=toCate.getParent();
			if(parent!=null){
				for(Category temp:toCate.getParent().getChildren()){
					if(temp.getPosition()>=toCate.getPosition()){
						temp.setPosition(temp.getPosition()+1);
						categoryRepository.save(temp);
					}
				}
			}else{
				for(Category temp:getRoots()){
					if(temp.getPosition()>=toCate.getPosition()){
						temp.setPosition(temp.getPosition()+1);
						categoryRepository.save(temp);
					}
				}
			}
			cur.setPosition(top);
		}else if("1".equals(type)){//之后
			cur.setParent(toCate.getParent());
			Category parent=toCate.getParent();
			if(parent!=null){
				for(Category temp:toCate.getParent().getChildren()){
					if(temp.getPosition()>toCate.getPosition()){
						temp.setPosition(temp.getPosition()+1);
						categoryRepository.save(temp);
					}
				}
			}else{
				for(Category temp:getRoots()){
					if(temp.getPosition()>toCate.getPosition()){
						temp.setPosition(temp.getPosition()+1);
						categoryRepository.save(temp);
					}
				}
			}
			cur.setPosition(toCate.getPosition()+1);
		}else{//之下
			cur.setParent(toCate);
			cur.setPosition(toCate.getChildren().size()+1);
		}
		
		
		categoryRepository.save(cur);
		
	}

	public static String mock(){
		JsonCategory c1=new JsonCategory();
		c1.setText("关于我们");
		JsonCategory c11=new JsonCategory();
		c11.setText("使用说明");
		JsonCategory c12=new JsonCategory();
		c12.setText("制度规范");
		JsonCategory c121=new JsonCategory();
		c121.setText("支撑体系");
		JsonCategory c122=new JsonCategory();
		c122.setText("科技部");
		List<JsonCategory> l12=new ArrayList<JsonCategory>();
		l12.add(c121);
		l12.add(c122);
		c12.setChildren(l12);
		List<JsonCategory> l1=new ArrayList<JsonCategory>();
		l1.add(c11);
		l1.add(c12);
		c1.setChildren(l1);
		
		List<JsonCategory> root=new ArrayList<JsonCategory>();
		root.add(c1);
		try {
			return JSONUtil.serialize(root, null, null, false, true);
		} catch (JSONException e) {
			return "";
		}
	}

	@Override
	public Set<Category> getUserCates(User loginUser) {
		return categoryRepository.getUserCates(loginUser);
	}
	
	
}
